/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-06-08 19:19:05
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-06-09 16:47:39
 * @FilePath: \ft_fan\User\Source\Hardware\FlashWrite.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*  -------------------------- (C) COPYRIGHT 2020 Fortiortech ShenZhen ---------------------------*/
/*  File Name      : FlashWrite.c
/*  Author         : Fortiortech  Appliction Team
/*  Version        : V1.0
/*  Date           : 2020-04-07
/*  Description    : This file contains XX-XX-XX function used for Motor Control.
/*  ----------------------------------------------------------------------------------------------*/
/*                                     All Rights Reserved
/*  ----------------------------------------------------------------------------------------------*/

#include <FU68xx_4.h>
#include <FU68xx_4_System.h>
#include <MyProject.h>

FlashSecurityKey data FlashKey;
uint8 MotorStatusSaveOnce = 0;

/*-------------------------------------------------------------------------------------------------
    Function Name :    void WriteXByte2Flash(uint8 xdata )
    Description   :    向目标FLASH中写入X字节的数据
    Input         :    uint8 xdata *FlashStartAddr：目标FLASH地址
                       uint32 NewData2Flash:        需要存储的数据 地址
                       uint8 XByte :                写字节数,XByte[1,255]
    Output        :读出的数据
-------------------------------------------------------------------------------------------------*/
static void WriteXByte2Flash(uint16 FlashStartAddr, uint32 NewData2Flash, uint8 XByte)
{
    //uint16 t_SaveData = 0;
    //uint16 t_SaveAddr = 0;
    uint8 i;
    uint32 tempofFlashData = 0;
    uint32 tempofNewFlashData = NewData2Flash;

    for (i = 0; i < XByte; i++)
    {
        //t_SaveAddr = FlashStartAddr + i;
        //t_SaveData = 8 * (XByte - i - 1);
        tempofFlashData = (tempofNewFlashData >> (8 * (XByte - i - 1))) & 0x000000ff;
        Flash_Sector_Write((FlashStartAddr + i), (uint8)tempofFlashData, FlashKey.x5a, FlashKey.x1f);
        _nop_();
    }
}

/*-------------------------------------------------------------------------------------------------
    Function Name :    uint16 GetLastDataFromFlash(uint8 xdata *BlockStartAddr)
    Description   :    从目标FLASH扇区读取2字节最新写入的数据
    Input         :    uint8 xdata *BlockStartAddr目标FLASH地址
    Output        :    读出的数据
-------------------------------------------------------------------------------------------------*/
static uint16 Get2ByteFromFlash(uint8 xdata *BlockStartAddr, uint8 DatNum_RS)
{
    uint8 xdata *FlashStartAddr = BlockStartAddr;
    uint8 i;
    uint16 tempofFlashData;
    uint8 DatNum = DatNum_RS;

    for (i = 0; i < 64; i++)
    {
        tempofFlashData = *(uint16 code *)(FlashStartAddr + 2 * i);
        if (tempofFlashData == 0)
        {
            if (i != 0)
            {
                if (DatNum > i)
                    DatNum = i;
                tempofFlashData = *(uint16 code *)(FlashStartAddr + 2 * (i - DatNum));
                return tempofFlashData;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            if (i == 63)
            {
                tempofFlashData = *(uint16 code *)(FlashStartAddr + 2 * (1 + i - DatNum));
                return tempofFlashData;
            }
        }
    }
    return 0;
}

/*  ----------------------------------------------------------------------------------------------*/
/*  Function Name  : Write2Byte2Flash
/*  Description    : 向目标扇区写2字节数据
/*  Date           : 2020-04-04
/*  Parameter      : BlockStartAddr: [目标Flash地址]
**             NewData2Flash: [被写入的数据]
/*  ----------------------------------------------------------------------------------------------*/
static void Write2Byte2Flash(uint16 BlockStartAddr, uint16 NewData2Flash)
{
    uint8 xdata *FlashStartAddr = BlockStartAddr;
    uint16 tempofFlashData = 0;
    uint8 i;

    for (i = 0; i < 64; i++)
    {
        tempofFlashData = *(uint16 code *)(FlashStartAddr + 2 * i);
        if (tempofFlashData == 0)
        {
            WriteXByte2Flash((FlashStartAddr + 2 * i), NewData2Flash, 2);
            break;
        }
        else
        {
            if (i == 63)
            {
                Flash_Sector_Erase(FlashStartAddr, FlashKey.x5a, FlashKey.x1f); // 扇区写满时擦除扇区
                _nop_();
                WriteXByte2Flash(FlashStartAddr, NewData2Flash, 2);
            }
        }
    }
}

/*  ----------------------------------------------------------------------------------------------*/
/*  Function Name  : UserCodeDeal
/*  Description    : 遥控地址码
/*  Date           : 2020-04-03
/*  Parameter      : None
/*  ----------------------------------------------------------------------------------------------*/
void UserCodeDeal(void)
{
    uint8 FlashWriteStatus = 0;

    /* 读取一次遥控地址码 */
    if (IRControl.FlagUserCodeRead)
    {
        IRScan.CurrentUserCode = Get2ByteFromFlash(USERCODEADDRESS, 1); // IRScan.CurrentUserCode
        IRControl.FlagUserCodeRead = 0;
    }
    /* 存储一次遥控器地址码 */
    if (IRControl.FlagUserCodeSave)
    {
        IRScan.CurrentUserCode = Get2ByteFromFlash(USERCODEADDRESS, 1);
        Write2Byte2Flash(USERCODEADDRESS, IRScan.UserCode);
        if ((IRScan.UserCode == IRScan.CurrentUserCode))
        {
            Write2Byte2Flash(USERCODEADDRESS, 0);
            IRScan.CurrentUserCode = Get2ByteFromFlash(USERCODEADDRESS, 1);
        }
        IRControl.FlagUserCodeSave = 0;
        Set_LED(150, 1);
    }
}

/*  ----------------------------------------------------------------------------------------------*/
/*  Function Name  : MotorStatusSave
/*  Description    : 电机参数存储
/*  Date           : 2020-04-04
/*  Parameter      : None
/*  ----------------------------------------------------------------------------------------------*/
void MotorStatusSave(void)
{
    uint16 TempMotorStatus = 0;
    uint16 TempStatus = 0;

    if (MotorStatusSaveOnce)
    {
        TempMotorStatus += 0x8000;
        TempStatus = 1;
        TempMotorStatus += (TempStatus << 13);

        TempStatus = IRControl.LEDONOFFStatus & 0x01;
        TempMotorStatus += (TempStatus << 14);

        TempStatus = mcFRState.OldTargetSpeed & 0x0F;
        TempMotorStatus += (TempStatus << 8);

        if ((Get2ByteFromFlash(FRSTATUSADDRESS, 1) != TempMotorStatus))
        {
            Write2Byte2Flash(FRSTATUSADDRESS, TempMotorStatus);
        }
        else
        {
            MotorStatusSaveOnce = 0;
            Write2Byte2Flash(FRSTATUSADDRESS, 0);
        }
    }
}

/*  ----------------------------------------------------------------------------------------------*/
/*  Function Name  : MotorStatusRead
/*  Description    : 电机状态读取
/*  Date           : 2020-04-07
/*  Parameter      : None
/*  ----------------------------------------------------------------------------------------------*/
void MotorStatusRead(void)
{
    uint16 TempMotorStatus = Get2ByteFromFlash(FRSTATUSADDRESS, 1);
    // 读出上一时刻的目标速度
    mcFRState.OldTargetSpeed = (TempMotorStatus >> 8) & 0x0F; //>>7
    IRControl.LEDONOFFStatus = (TempMotorStatus >> 14) & 0x01;
    mcFRState.FR = CW;

    if (IRControl.LEDONOFFStatus == 1)
    {
        Set_LED(70, 0xF1);
    }
    else
    {
        Set_LED(300, 0xF1);
    }
}
